﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;

namespace ProjectBase.Utils
{
    public class CRequest
    {
        public CRequest(){}

		#region	 tra ve gia trị tham so
		public static string GetQueryUri()
		{			
			if(HttpContext.Current.Request.QueryString!=null &&
				HttpContext.Current.Request.QueryString.ToString()!="")
				return HttpContext.Current.Request.QueryString.ToString();
			else
				return "";
		}        
		#endregion

		#region	 tra ve gia trị tham so
        public static string GetQueryUri(string paraName, string defaultValue)
		{
            String sUrl = "";
            if (HttpContext.Current.Request.QueryString[paraName] != null &&
                HttpContext.Current.Request.QueryString[paraName].ToString() != "")
            {
                sUrl = HttpContext.Current.Request.QueryString[paraName].ToString();
                sUrl = Utils.FitlerBadString(sUrl);
                return sUrl;
            }
            else
                return defaultValue;
		}

        public static string GetQueryUriNoFitlerBad(string paraName, string defaultValue)
        {
            String sUrl = "";
            if (HttpContext.Current.Request.QueryString[paraName] != null &&
                HttpContext.Current.Request.QueryString[paraName].ToString() != "")
            {
                sUrl = HttpContext.Current.Request.QueryString[paraName].ToString();
                return sUrl;
            }
            else
                return defaultValue;
        }

        public static string GetForm(string paraName)
        {
            if (HttpContext.Current.Request.Form.Get(paraName) != null &&
                HttpContext.Current.Request.Form.Get(paraName) != "")
                return HttpContext.Current.Request.Form.Get(paraName).ToString();
            else
                return "";
        }
		#endregion

		#region	 cong them tham so vao uri
		public string AddQueryUri(string paraName, string paraValue)
		{	
			string reqValue = HttpContext.Current.Request.QueryString.ToString();

			if (reqValue.Equals(""))
			{
				return paraName + "=" + paraValue;

			}
			else
			{
				return reqValue + "&" + paraName + "=" + paraValue;

			}			
		}
		#endregion

		#region	 kiem tra va bo sung toan tu dau tien cua request
		public string AppendSymbolUri()
		{	
			string reqValue = HttpContext.Current.Request.QueryString.ToString();

			if (reqValue.Equals(""))
			{
				return "?";

			}
			else
			{
				return "?";

			}			
		}
		#endregion

		#region	 xoa tham so tu uri
        public static string DelQueryUri(string paraName, string paraValue)
		{	
			string replaceString = paraName + "=" + paraValue;
			string reqValue = HttpContext.Current.Request.QueryString.ToString();

			if (reqValue.Equals(""))
			{
				return "";

			}
			else
			{				
				return reqValue.Replace("&" + replaceString, String.Empty)
					.Replace(replaceString + "&", String.Empty)
					.Replace(replaceString, String.Empty);
			}			
		}
		#endregion

        #region Thay the va xoa tham so tren url
        public static string UpdateUrl(string url, Dictionary<string, string> queryStringReplacement, ICollection<string> queryStringRemoval)
        {
            string header, queryString;
            int index = url.IndexOf('?');
            if (-1 == index)
            { // not found
                header = url;
                queryString = string.Empty;
            }
            else
            {
                header = url.Substring(0, index);
                queryString = url.Substring(index + 1);
            }
            Dictionary<string, bool> replaced = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
            Dictionary<string, string> newQueryString = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            foreach (string pair in queryString.Split('&'))
            {
                if (!string.IsNullOrEmpty(pair))
                {
                    string[] parts = pair.Split('=');
                    if (parts.Length > 0)
                    {
                        string key = parts[0];
                        if (queryStringReplacement.ContainsKey(key))
                        {
                            newQueryString[key] = queryStringReplacement[key];
                            replaced[key] = true;
                        }
                        else
                            newQueryString[key] = parts.Length > 1 ? parts[1] : string.Empty;
                    }
                }
            }
            //add non replaced query string
            foreach (string key in queryStringReplacement.Keys)
            {
                if (!replaced.ContainsKey(key))
                {
                    newQueryString[key] = queryStringReplacement[key];
                }
            }
            Dictionary<string, bool> removalKeys = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
            if (null != queryStringRemoval)
            {
                foreach (string key in queryStringRemoval)
                {
                    removalKeys[key] = true;
                }
            }

            StringBuilder sb = new StringBuilder(header);
            bool first = true;
            foreach (string key in newQueryString.Keys)
            {
                if (!removalKeys.ContainsKey(key))
                {
                    if (first)
                    {
                        sb.Append("?");
                        first = false;
                    }
                    else
                        sb.Append("&");
                    sb.Append(key);
                    if (!string.IsNullOrEmpty(newQueryString[key]))
                        sb.Append("=").Append(newQueryString[key]);
                }
            }
            return sb.ToString();
        }
        #endregion
    }
}
